/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

import Modelo.DAL.Bono_DAO;
import Acceso.Security;
import org.json.*;
import java.io.PrintWriter;
import Modelo.DAL.ConexionBD;
import java.sql.*;

/**
 *
 * @author yngrdyn
 */
public class consultarBonosPersonalAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         ActionErrors errors = new ActionErrors();

         String[] a = {"Administrador","Director DSI","Personal Administrativo"};
       if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }
        System.out.println("Paso Autenticación");

         String TipoBono = request.getParameter("TipoBono");

        if(TipoBono.equals("seleccionar")){
         errors.add("tipoBono_Esrequerido", new ActionMessage("errors.tipoBono_Esrequerido"));
         saveErrors(request, errors);
         return mapping.findForward(FAILURE);
        }

        String[] arrayTipoBono = TipoBono.split("/");

        String id_bono = arrayTipoBono[0];
        String tipo = arrayTipoBono[1];
        String sede = arrayTipoBono[2];

       

        response.setContentType("text/json");
        JSONObject data = new JSONObject();
        System.out.println("\n\n\nEntra al action");
        Bono_DAO bono = new Bono_DAO();
        data.put("listaBonos", bono.consultarTipoBono(id_bono,sede));
        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
